﻿using System;
using System.ComponentModel;
using System.Web.Mvc;
using FsCarInfo.Core.Entity;
using FsCarInfo.Facade;
using HMIS.FrameWork;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	/// 公务用车派车单管理页面
	/// </summary>
	public class BCarSendController : BaseController
	{
		private readonly IBCarSendFacade BCarSendFacade;
		private readonly IBCarFacade BCarFacade;
		private readonly IHospUserFacade HospUserFacade;
		//private readonly IActionPermissionFacade ActionPermissionFacade;

		public BCarSendController(IBCarSendFacade BCarSendFacade 
			,IHospUserFacade HospUserFacade
			,IBCarFacade BCarFacade)
		{
			this.BCarSendFacade = BCarSendFacade;
			this.HospUserFacade = HospUserFacade;
			this.BCarFacade = BCarFacade;
			//this.ActionPermissionFacade = ActionPermissionFacade;
		}

		[Description("获取公务用车派车单列表")]
		public ActionResult GetPlistAll()
		{
			long total;
			var data = BCarSendFacade.GetPlistAll(
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			return this.JsonFormat(
				new ExtGirdData<BCarSend> { total = total, data = data },
				new string[] { "CustomType" });
		}

		[Description("访问公务用车派车单管理页面")]
		[ViewPage]
		public ActionResult BCarSend()
		{
			return View();
		}

		[ExtResult]
		[Description("添加修改公务用车派车单信息")]
		public ActionResult SaveOrUpdate()
		{
			BCarSend entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new BCarSend
				{
					ApplicantID = Request["ApplicantID"],
					Applicant = Request["Applicant"],
					ApplyDate = Convert.ToDateTime(Request["ApplyDate"]),
					BCar = BCarFacade.GetById(Request["BCarID"]),
					BCarNo = Request["BCarNo"],
					DriverID = Request["DriverID"],
					Driver = Request["Driver"],
					ApplyFor = Request["ApplyFor"],
					Type = Convert.ToInt32(Request["Type"]),
					PlanLine = Request["PlanLine"],
					MileBeg = Convert.ToInt32(Request["MileBeg"]),
					MileEnd = Convert.ToInt32(Request["MileEnd"]),
					Mileage = Convert.ToInt32(Request["Mileage"]),
					ExpOutTime = Convert.ToDateTime(Request["ExpOutTime"]),
					ExpInTime = Convert.ToDateTime(Request["ExpInTime"]),
					ActOutTime = Request["ActOutTime"],
					ActInTime = Request["ActInTime"],
					TT_ContUs = Request["TT_ContUs"],
					TT_ContIDD = Request["TT_ContIDD"],
					TT_CustCarNo = Request["TT_CustCarNo"],
					TT_State = Request["TT_State"],
					TT_Reason = Request["TT_Reason"],
					TT_Fee = Convert.ToDecimal(Request["TT_Fee"]),
					Description = Request["Description"]
				};
			}
			else
			{
				entity = BCarSendFacade.Get(Request["Id"]);
				entity.ApplicantID = Request["ApplicantID"];
				entity.Applicant = Request["Applicant"];
				entity.ApplyDate = Convert.ToDateTime(Request["ApplyDate"]);
				entity.BCar = BCarFacade.GetById(Request["BCarID"]);
				entity.BCarNo = Request["BCarNo"];
				entity.DriverID = Request["DriverID"];
				entity.Driver = Request["Driver"];
				entity.ApplyFor = Request["ApplyFor"];
				entity.Type = Convert.ToInt32(Request["Type"]);
				entity.PlanLine = Request["PlanLine"];
				entity.MileBeg = Convert.ToInt32(Request["MileBeg"]);
				entity.MileEnd = Convert.ToInt32(Request["MileEnd"]);
				entity.Mileage = Convert.ToInt32(Request["Mileage"]);
				entity.ExpOutTime = Convert.ToDateTime(Request["ExpOutTime"]);
				entity.ExpInTime = Convert.ToDateTime(Request["ExpInTime"]);
				entity.ActOutTime = Request["ActOutTime"];
				entity.ActInTime = Request["ActInTime"];
				entity.TT_ContUs = Request["TT_ContUs"];
				entity.TT_ContIDD = Request["TT_ContIDD"];
				entity.TT_CustCarNo = Request["TT_CustCarNo"];
				entity.TT_State = Request["TT_State"];
				entity.TT_Reason = Request["TT_Reason"];
				entity.TT_Fee = Convert.ToDecimal(Request["TT_Fee"]);
				entity.Description = Request["Description"];
			}
			// 保存信息
			BCarSendFacade.SaveOrUpdate(entity);

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}

		[Description("获取公务用车派车单信息")]
		public ActionResult GetBCarSendById()
		{
			var entity = BCarSendFacade.Get(Request["Id"]);
			return this.JsonFormat(new ExtFormLoadData<BCarSend> { data = new BCarSend[] { entity } });
		}

		[ExtResult]
		[Description("删除公务用车派车单的基本资料")]
		public ActionResult Delete()
		{
			BCarSendFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}
	}
}
